package com.culturalCenter.dataCenter.service.impl.activity;

import com.culturalCenter.dataCenter.customEntity.activity.CustomActivityCommentList;
import com.culturalCenter.dataCenter.customEntity.activity.CustomActivityNumber;
import com.culturalCenter.dataCenter.customEntity.responseParam.activity.ActivityPopularAnalysisResponse;
import com.culturalCenter.dataCenter.entity.activity.ActivityCommentCount;
import com.culturalCenter.dataCenter.entity.activity.ActivityInfoCount;
import com.culturalCenter.dataCenter.mapper.activity.ActivityCommentCountMapper;
import com.culturalCenter.dataCenter.mapper.activity.ActivityCommentMapper;
import com.culturalCenter.dataCenter.mapper.activity.ActivityInfoCountMapper;
import com.culturalCenter.dataCenter.service.activity.ActivityPopularAnalysisService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @Author zhao
 * @Date 2020/11/4 14:12
 */
@Service
public class ActivityPopularAnalysisServiceImpl implements ActivityPopularAnalysisService {

    @Resource
    private ActivityInfoCountMapper activityInfoCountMapper;
    @Resource
    private ActivityCommentCountMapper activityCommentCountMapper;
    @Resource
    private ActivityCommentMapper activityCommentMapper;

    @Override
    public ActivityPopularAnalysisResponse getPopularAnalysisData(String activityId) {
        ActivityPopularAnalysisResponse activityPopularAnalysisResponse = new ActivityPopularAnalysisResponse();
        List<ActivityInfoCount> browseList = activityInfoCountMapper.getApplicantNumberByActivityId(activityId);
        activityPopularAnalysisResponse.setViewNumber(browseList);
        //活动人数情况
        List<CustomActivityNumber> activityNumberList = activityInfoCountMapper.queryActivitiesByActivityId(activityId);
        if (CollectionUtils.isNotEmpty(activityNumberList)) {
            activityNumberList.forEach(a -> {
                if (Objects.nonNull(a.getAirborneNum()) && a.getAirborneNum() < 0) {
                    a.setAirborneNum(0);
                    a.setAirborneRate("0");
                }
                double v = Double.parseDouble(Optional.ofNullable(a.getExceedRate()).orElse("0"));
                double num =(1-Math.exp(-v))*100;
                DecimalFormat decimalFormat = new DecimalFormat("#.##");
                a.setExceedRate(decimalFormat.format(num));
            });
            activityPopularAnalysisResponse.setActivityNumberList(activityNumberList);
            //activityPopularAnalysisResponse.setAirborneList(activityNumberList);
        }
        //List<ActivityCommentCount> activityRank = activityCommentCountMapper.selectActivityRank(branchId);

        //评星及评星比例
        List<ActivityCommentCount> gradeList = activityCommentCountMapper.queryCommentsByActivityId(activityId);
        if (CollectionUtils.isNotEmpty(gradeList)) {
            gradeList.forEach(activityCommentCount -> {
                //总评星人数
                Integer totalPeopleRating = activityCommentCount.getFiveStar() + activityCommentCount.getFourPointFiveStar() + activityCommentCount.getFourStar()
                        + activityCommentCount.getThreePointFiveStar() + activityCommentCount.getThreeStar() + activityCommentCount.getTwoPointFiveStar()
                        + activityCommentCount.getTwoStar() + activityCommentCount.getOnePointFiveStar() + activityCommentCount.getOneStar() + activityCommentCount.getZeroPointFiveStar();

                DecimalFormat df = new DecimalFormat("0.00");
                if (totalPeopleRating != 0) {
                    //每个评星等级的比例
                    activityCommentCount.setFourPointFiveStarRate(Double.valueOf(df.format((double) activityCommentCount.getFourPointFiveStar() / totalPeopleRating * 100)));
                    activityCommentCount.setFourStarRate(Double.valueOf(df.format((double) activityCommentCount.getFourStar() / totalPeopleRating * 100)));
                    activityCommentCount.setThreePointFiveStarRate(Double.valueOf(df.format((double) activityCommentCount.getThreePointFiveStar() / totalPeopleRating * 100)));
                    activityCommentCount.setThreeStarRate(Double.valueOf(df.format((double) activityCommentCount.getThreeStar() / totalPeopleRating * 100)));
                    activityCommentCount.setTwoPointFiveStarRate(Double.valueOf(df.format((double) activityCommentCount.getTwoPointFiveStar() / totalPeopleRating * 100)));
                    activityCommentCount.setTwoStarRate(Double.valueOf(df.format((double) activityCommentCount.getTwoStar() / totalPeopleRating * 100)));
                    activityCommentCount.setOnePointFiveStarRate(Double.valueOf(df.format((double) activityCommentCount.getOnePointFiveStar() / totalPeopleRating * 100)));
                    activityCommentCount.setOneStarRate(Double.valueOf(df.format((double) activityCommentCount.getOneStar() / totalPeopleRating * 100)));
                    activityCommentCount.setZeroPointFiveStarRate(Double.valueOf(df.format((double) activityCommentCount.getZeroPointFiveStar() / totalPeopleRating * 100)));
                    //平均评星
                    DecimalFormat df1 = new DecimalFormat("0.0");
                    double totalStar = activityCommentCount.getFiveStar() * 5 + activityCommentCount.getFourPointFiveStar() * 4.5 + activityCommentCount.getFourStar() * 4
                            + activityCommentCount.getThreePointFiveStar() * 3.5 + activityCommentCount.getThreeStar() * 3 + activityCommentCount.getTwoPointFiveStar() * 2.5
                            + activityCommentCount.getTwoStar() * 2 + activityCommentCount.getOnePointFiveStar() * 1.5 + activityCommentCount.getOneStar() + activityCommentCount.getZeroPointFiveStar() * 0.5;
                    activityCommentCount.setAvgStar(Double.parseDouble(df1.format(totalStar / totalPeopleRating)));
                } else {
                    activityCommentCount.setFourPointFiveStarRate(0.00);
                    activityCommentCount.setFourStarRate(0.00);
                    activityCommentCount.setThreePointFiveStarRate(0.00);
                    activityCommentCount.setThreeStarRate(0.00);
                    activityCommentCount.setTwoPointFiveStarRate(0.00);
                    activityCommentCount.setTwoStarRate(0.00);
                    activityCommentCount.setOnePointFiveStarRate(0.00);
                    activityCommentCount.setOneStarRate(0.00);
                    activityCommentCount.setZeroPointFiveStarRate(0.00);
                    activityCommentCount.setAvgStar(0.0);
                }

            });
            activityPopularAnalysisResponse.setGradeList(gradeList);
        }
        //点赞及比例
        List<ActivityCommentCount> likeRatioList = activityCommentCountMapper.queryLikeByActivityId(activityId);
        if (CollectionUtils.isNotEmpty(likeRatioList)) {
            //计算点赞比例
            likeRatioList.forEach(ActivityCommentCount -> {
                DecimalFormat df = new DecimalFormat("0.00");
                ActivityCommentCount.setLikeRate(ActivityCommentCount.getBrowseNum() != 0 ?
                        df.format((double) ActivityCommentCount.getLikeNum() / ActivityCommentCount.getBrowseNum() * 100) : "0.00");
            });
            activityPopularAnalysisResponse.setLikeRatioList(likeRatioList);
        }

        /*//点赞列表
        List<CustomInteractive> likeList = activityCommentCountMapper.queryByActivityId(activityId);
        if (CollectionUtils.isNotEmpty(likeList)) {
            activityPopularAnalysisResponse.setLikeList(likeList);
        }*/

        return activityPopularAnalysisResponse;
    }

    @Override
    public List<CustomActivityCommentList> getCommentList(String activityId) {
        return activityCommentMapper.queryListByActivityId(activityId);
    }

}
